<?php

declare (strict_types=1);

namespace app\api\service;

use app\common\service\bangdou\Common as CommonService;
use app\common\service\bangdou\BdMember as BdMemberService;
use app\common\service\BaseService;
use app\common\library\helper;
use cores\exception\BaseException;

/**
 * 用户邦豆处理
 * Class UserBd
 * @package app\api\service;
 */
class UserBd extends BaseService
{
    /**
     * 获取邦豆生活的三券
     * @param $bd_uid 邦豆生活的用户Id
     */
    public static function getBdCoupon($bd_uid)
    {
        $user_property = array();
        if($bd_uid>0){
            $user_property = BdMemberService::getUserProperty($bd_uid);
        }
        $user['bangdouCouponAmount'] = isset($user_property['bangdouCouponAmount']) ? $user_property['bangdouCouponAmount'] : 0;//邦豆券
        $user['cashCouponAmount'] = isset($user_property['cashCouponAmount']) ? $user_property['cashCouponAmount'] : 0;//现金券
        $user['cashGiftCouponAmount'] = isset($user_property['cashGiftCouponAmount']) ? $user_property['cashGiftCouponAmount'] : 0;//礼品卡
        $user['memberType'] = $user_property['memberType'] ?? 1;//1普通会员  2、黄金会员 3、铂金会员 4、钻石会员
        //$user['nick_name'] = $user_property['nick_name'];//会员昵称
        $user['me_start_time'] = '';
        $user['me_end_time'] = '';
        if(isset($user_property['me_start_time']) && $user_property['me_start_time']){
            list($first_start, $rest) = explode(' ', $user_property['me_start_time'], 2);
            $user['me_start_time'] = $first_start;
        }
        if(isset($user_property['me_end_time']) && $user_property['me_end_time']){
            list($first_end, $rest) = explode(' ', $user_property['me_end_time'], 2);
            $user['me_end_time'] = $first_end;
        }
        $user['bd_uid'] = $bd_uid;
        return $user;
    }


    /**
     * 计算订单抵扣金额和支付金额
     * @param $orderPayPrice 订单实际支付金额
     * @param $payType 使用三券类型
     * @param $bd_coupon 用户的三券金额
     * @param $unsupportedBdPrice 订单不支持三券金额
     * @return array
     * @throws BaseException
     */
    public static function countAmount($orderPayPrice,$payType,$bd_coupon,$unsupportedBdPrice='0')
    {
        if($orderPayPrice <=0){
            throwError('支付金额必须大于0');
        }
        if($orderPayPrice < $unsupportedBdPrice){
            throwError('订单金额有误');
        }

        $dk_money = '0.00';
        $pay_money = '0.00';
        $orderPayPrice = bcsub($orderPayPrice, $unsupportedBdPrice, 2);
        if($payType == 10){//余额支付
            $bd_money = $bd_coupon['balance'] ?? 0.00;
            if($bd_money >= $orderPayPrice){
                $dk_money = $orderPayPrice;
            }else{
                $pay_money = bcsub($orderPayPrice, $bd_money, 2);
                $dk_money = $bd_money;
            }
        }elseif($payType == 7){//邦豆抵扣
            $bd_money = $bd_coupon['bangdouCouponAmount'] ?? 0.00;
            if($bd_money >= $orderPayPrice){
                $dk_money = $orderPayPrice;
            }else{
                $pay_money = bcsub($orderPayPrice, $bd_money, 2);
                $dk_money = $bd_money;
            }
        }elseif ($payType == 6){//现金券抵扣
            $bd_money = $bd_coupon['cashCouponAmount'] ?? 0.00;
            $moneyCoupon = bcmul($orderPayPrice,'0.5',2);
            if($bd_money >= $moneyCoupon) {
                $pay_money = bcsub($orderPayPrice, $moneyCoupon, 2);
                $dk_money = $moneyCoupon;
            }else{
                $pay_money = bcsub($orderPayPrice, $bd_money, 2);
                $dk_money = $bd_money;
            }
        }elseif ($payType == 5){//礼金券
            $bd_money = $bd_coupon['cashGiftCouponAmount'] ?? 0.00;
            $moneyCoupon = bcmul($orderPayPrice,'0.25',2);
            if($bd_money >= $moneyCoupon) {
                $pay_money = bcsub($orderPayPrice, $moneyCoupon, 2);
                $dk_money = $moneyCoupon;
            }else{
                $pay_money = bcsub($orderPayPrice, $bd_money, 2);
                $dk_money = $bd_money;
            }
        }else{
            $pay_money = $orderPayPrice;
            $dk_money = '0.00';
        }
        $pay_money = bcadd($pay_money,$unsupportedBdPrice,2);
        return ['dk_money'=>$dk_money,'pay_money'=>$pay_money];
    }

    /**
     * 用户订单商品信息处理
     * @param $orderGoods
     * @return array
     */
    public static function getUserOrderUnsupportedBd($orderGoods){
        $sum = '0.00';
        if($orderGoods && is_array($orderGoods)){
            foreach ($orderGoods as $key=>$item){
                if(isset($item['is_bd']) && $item['is_bd']==2){//商品不支持三券
                    $sum = bcadd($sum, $item['total_pay_price'], 2);
                }
            }
        }
        $data['unsupportedBdPrice'] = $sum;
        return $data;
    }

}
